package org.wangbao.web;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.lucene.index.CorruptIndexException;
import org.junit.Test;
import org.wangbao.model.PubUrlArg;
import org.wangbao.service.ContentParserService;
import org.wangbao.service.VersionControlService;

import com.opensymphony.xwork2.ActionSupport;

public class BuildFlashAction extends ActionSupport {

	// 图片地址对应期刊号
	private String versionId="A02";
	
	private String journalId="20100605";

	// 新闻块配置
	private String blockConfig;
	
	private String linkConfig;
	
	private String xmlUrl="http://cubeimg.zhongsou.com/library/A4FMKD/06$16/A19GJJD616C.xml";
	
	//还多出一个需要替换的东西 xmlPath;
	
//	= "[{\"versionId\":\"A03\",\"view\":{\"width\":'251',\"height\":'116',\"y\":'32',\"scale\":'0.24496644295302014',\"x\":'74'}," +
//    "\"textId\":\"A3FMZD200.txt\",\"journalId\":\"20100605\",\"linkList\":\"test\",\"titleId\":\"A3FMZD504C33.txt\"},{" +
//    "\"versionId\":\"A03\",\"view\":{\"width\":'254',\"height\":'83',\"y\":'156',\"scale\":'0.24496644295302014',\"x\":'70'},\"textId\":\"A3FMZD201.txt\",\"journalId\":\"20100605\"," +
//    "\"linkList\":'',\"titleId\":\"A3FMZD504C27.txt\"}]";

	// 相关链接新闻id
	private List linkIdList;
	
	private String xmlcPath;
	
	private String flexPath;
	
	private String tmpPath;
	
	private String outPath;
	
	private String puarg;
	
	//private PropertyPlaceholderConfigurer propertyConfigurer;

	String cmds ;
//	String cmds = "java -jar D:/MyDev/Runtime/flex4/lib/mxmlc.jar " +
//			"+flexlib D:\\MyDev\\Runtime\\flex4\\frameworks " +
//			"D:\\FlashWorkSpace\\genswf\\src\\genswf.mxml " +
//			"-output=D:\\FlashWorkSpace\\genswf\\bin-debug\\genswf.swf";

	String path;

	public ContentParserService contentParserService;
	public VersionControlService versionControlService;

	private String uploadSwf;
	
	private String picPath="http://cubeimg.zhongsou.com/library/A4FMKD/06$16/A4FMKD616C.jpg";
	
	private String swfPath;

	/**
	 * According to the block configuration, do the replacement job 
	 * Complete the replacement task in contentParserService build method 
	 * @throws Exception 
	 */
	public String execute() throws Exception{
		
		System.out.println("--------"+picPath);

		//contentParserService.buildFlash(blockConfig,versionId,journalId,picPath);
		contentParserService.buildFlash2(xmlUrl,puarg,versionId,journalId,picPath);
		xmlcPath = contentParserService.handleWebPath()+"flexlib/lib/mxmlc.jar ";
		flexPath = "+flexlib "+contentParserService.handleWebPath()+"flexlib/frameworks ";
		tmpPath = contentParserService.handleWebPath()+"genswf/src/exec.mxml ";
		
		swfPath = "genswf/bin-debug/"+journalId+"-"+versionId+".swf";
		
		outPath = "-output="+contentParserService.handleWebPath()+swfPath;
		cmds = "java -jar "+xmlcPath+flexPath+tmpPath+outPath;
		executeCommand();
		System.out.println("path is :"+cmds);
		
		return "path";
		
		// 原本的通过文件上传进行pdf生成
		// try {
		//
		// InputStream streamIn = new BufferedInputStream(new FileInputStream(
		// uploadSwf));
		//
		// ActionContext ac = ActionContext.getContext();
		//
		// ServletContext sc = (ServletContext) ac
		// .get(ServletActionContext.SERVLET_CONTEXT);
		//
		// path = sc.getRealPath("/");
		//
		// OutputStream streamOut = new FileOutputStream(path + "/swfName");
		//
		// int byteRead = 0;
		//
		// byte[] buffer = new byte[8192];
		//
		// while ((byteRead = streamIn.read(buffer, 0, 8192)) != -1) {
		// streamOut.write(buffer, 0, byteRead);
		// }
		//
		// System.out.println("upload is over.." + path);
		//			
		// executeCommand();
		//
		// streamOut.close();
		// streamIn.close();
		//
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

	}
	
	
	public String queryBlock() throws CorruptIndexException, IOException{
		linkConfig = contentParserService.queryBlock(journalId,versionId);
		return "link";
	}

	
	

	@Test
	public void executeCommand() {
		try {
			Process ps = Runtime.getRuntime().exec(cmds);
			System.out.print(loadStream(ps.getInputStream()));
			System.err.print(loadStream(ps.getErrorStream()));
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	// read an input-stream into a String
	String loadStream(InputStream in) throws IOException {
		int ptr = 0;
		in = new BufferedInputStream(in);
		StringBuffer buffer = new StringBuffer();
		while ((ptr = in.read()) != -1) {
			buffer.append((char) ptr);
		}
		return buffer.toString();
	}

	public String getCmds() {
		return cmds;
	}

	public void setCmds(String cmds) {
		this.cmds = cmds;
	}

	public String getVersionId() {
		return versionId;
	}

	public void setVersionId(String versionId) {
		this.versionId = versionId;
	}

	public String getBlockConfig() {
		return blockConfig;
	}

	public void setBlockConfig(String blockConfig) {
		this.blockConfig = blockConfig;
	}

	public List getLinkIdList() {
		return linkIdList;
	}

	public void setLinkIdList(List linkIdList) {
		this.linkIdList = linkIdList;
	}

	public String getXmlcPath() {
		return xmlcPath;
	}

	public void setXmlcPath(String xmlcPath) {
		this.xmlcPath = xmlcPath;
	}

	public String getFlexPath() {
		return flexPath;
	}

	public void setFlexPath(String flexPath) {
		this.flexPath = flexPath;
	}

	public String getTmpPath() {
		return tmpPath;
	}

	public void setTmpPath(String tmpPath) {
		this.tmpPath = tmpPath;
	}

	public String getOutPath() {
		return outPath;
	}

	public void setOutPath(String outPath) {
		this.outPath = outPath;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public ContentParserService getContentParserService() {
		return contentParserService;
	}

	public void setContentParserService(ContentParserService contentParserService) {
		this.contentParserService = contentParserService;
	}

	public VersionControlService getVersionControlService() {
		return versionControlService;
	}

	public void setVersionControlService(VersionControlService versionControlService) {
		this.versionControlService = versionControlService;
	}

	public String getUploadSwf() {
		return uploadSwf;
	}

	public void setUploadSwf(String uploadSwf) {
		this.uploadSwf = uploadSwf;
	}

	public String getPicPath() {
		return picPath;
	}

	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}

	public String getSwfPath() {
		return swfPath;
	}

	public void setSwfPath(String swfPath) {
		this.swfPath = swfPath;
	}

	public String getJournalId() {
		return journalId;
	}

	public void setJournalId(String journalId) {
		this.journalId = journalId;
	}


	public String getLinkConfig() {
		return linkConfig;
	}


	public void setLinkConfig(String linkConfig) {
		this.linkConfig = linkConfig;
	}

	public String getXmlUrl() {
		return xmlUrl;
	}


	public void setXmlUrl(String xmlUrl) {
		this.xmlUrl = xmlUrl;
	}


	public void setPuarg(String puarg) {
		this.puarg = puarg;
	}
}
